package cn.zyl.functions;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by weili0677 on 2016/8/5.
 */
public class TextTools {


    /**
     * 日期加减格式处理
     * @param text
     * @return 加减过后的日期
     */
    public static Date dateCompute(String text, Date now){
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        if (text.contains("-")){
            //减
            String format = text.substring(text.indexOf("(")+1,text.indexOf(")"));
            if (format.contains("dd")){
                //天
                int num = Integer.parseInt(format.replace("dd",""));  //几天
                cal.add(Calendar.DAY_OF_MONTH,-num);
            }else if (format.contains("MM")){
                //月
                int num = Integer.parseInt(format.replace("MM",""));  //几月
                cal.add(Calendar.MONTH,-num);
            }
        }else if(text.contains("+")){
            //加
            String format = text.substring(text.indexOf("(")+1,text.indexOf(")"));
            if (format.contains("dd")){
                //天
                int num = Integer.parseInt(format.replace("dd",""));  //几天
                cal.add(Calendar.DAY_OF_MONTH,num);
            }else if (format.contains("MM")){
                //月
                int num = Integer.parseInt(format.replace("MM",""));  //几月
                cal.add(Calendar.MONTH,num);
            }
        }
        return cal.getTime();
    }
}
